package com.outsider.util.thunder;

import java.awt.AWTException;
import java.awt.Desktop;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.eclipse.jetty.util.log.Log;

import com.jfinal.kit.PropKit;

/**
 * @author outsider
 * @Description 迅雷类 单例 默认配置文件thunderConfig.txt
 * 
 */
public class Thunder {
	private String loc;
	private static Thunder instance;
	private int startButtonX;
	private int startButtonY;
	private boolean isRuning = false;

	public static Thunder getInstance() {
		if (instance == null)
			synchronized (Thunder.class) {
				if (instance == null)
					instance = new Thunder();
			}
		return instance;
	}

	private Thunder() {
		InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("thunderConfig.txt");
		Properties properties = new Properties();
		try {
			properties.load(inputStream);
			loc = properties.getProperty("loc");
			Logger.getLogger("").info("Thunder instance init success");
			startUp();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void searchStartButtonLoc() {

		FindPic findPic = new FindPic(FindPic.class.getClassLoader().getResource("button.bmp").getPath());
		Integer[] loc = findPic.getFirstLoc();
		startButtonX = loc[0];
		startButtonY = loc[1];
		Logger.getLogger("").info("Thunder Button Loc is" + loc[0] + " " + loc[1]);

	}

	public void startUp() {
		callUp();
		Logger.getLogger("").info("Thunder Start-up");
	}

	public void shutdown() {
		if (isRuning == true)
			try {
				Runtime.getRuntime().exec("taskkill /F /IM Thunder.exe");
				Logger.getLogger("").info("Thunder has been shutdown");
				isRuning = false;
				Thread.sleep(100);
			} catch (IOException | InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	
	public void reboot(){
		shutdown();
		startUp();
	}

	
	/**
	 * 开始链接任务</Br>
	 * 并发控制
	 */
	public synchronized void startLinkTask(String url) {
/*		// 调出迅雷主界面
		callUp();
		// 复制url到剪贴板
		Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
		clipboard.setContents(new StringSelection(url), null);
		try {
			Robot robot = new Robot();
			robot.keyPress(KeyEvent.VK_CONTROL);
			robot.keyPress(KeyEvent.VK_N);
			Thread.sleep(100);
			robot.keyRelease(KeyEvent.VK_CONTROL);
			robot.keyRelease(KeyEvent.VK_N);
			Thread.sleep(500);
			
			robot.keyPress(KeyEvent.VK_CONTROL);
			robot.keyPress(KeyEvent.VK_V);
			Thread.sleep(100);
			robot.keyRelease(KeyEvent.VK_CONTROL);
			robot.keyRelease(KeyEvent.VK_V);
			while (startButtonX==0)
				searchStartButtonLoc();
			if (startButtonX==0)
			  {
				startButtonX=650;
				startButtonY=372;
			  }
			
			robot.mouseMove(startButtonX, startButtonY);
			robot.mousePress(InputEvent.BUTTON1_MASK);
			robot.mouseRelease(InputEvent.BUTTON1_MASK);
			Thread.sleep(1000);
		} catch (AWTException | InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		try {
			Runtime.getRuntime().exec(loc+" "+url);
			Thread.sleep(3000);
		} catch (IOException | InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	private void callUp() {
		try {
		if (isRuning == false){

				Desktop.getDesktop().open(new File(loc));
				Thread.sleep(1000);
				isRuning = true;
		}
		else
			Thread.sleep(100);
			} catch (IOException | InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

	}
	public static String transState(int state){
		if (state==8||state==10)
			return "完成";
		if (state==5)
			return "进行中";
		else 
			return "暂停";
	}

}
